﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Text;

using Leafing.Data;
using Leafing.Core;
using Leafing.Web;

using Wz16.Baoming.BLL;
using Leafing.Core.Logging;

public partial class frmBaoming : PageBase
{

    protected void Page_Load(object sender, EventArgs e)
    {

        //DbEntry.DropAndCreate(typeof(BmInfo));
        if (!Page.IsPostBack)
        {
            this.MultiView1.ActiveViewIndex = 1;
            if (!Request.IsAuthenticated)
            {
      //          this.MessageBox("网络报名已经结束，请于7月1日上我校校园网查看材料审核结果。");
            }
            else
            {
                this.MultiView1.ActiveViewIndex = 0;
            }

            if (Request.QueryString["Search"] != null && Request.QueryString["Search"].ToString() == "true")
            {
                this.MultiView1.ActiveViewIndex = 1;
            }
            else
            {
                
            }
            BindData();
        }
    }

    /// <summary>
    /// 记录ID
    /// </summary>
    public long BmInfoId
    {
        get
        {
            if (this.Request.IsAuthenticated && Request.QueryString["id"] != null && !string.IsNullOrEmpty(this.Request.QueryString["id"].ToString())
                && ViewState["BmInfoId"] == null)
            {
                ViewState["BmInfoId"] = long.Parse(Request.QueryString["id"].ToString());
            }
            else if (this.Request.IsAuthenticated != true && Request.QueryString["id"] != null)
            {
                Response.Redirect("frmBaoming.aspx");
            }
            if (ViewState["BmInfoId"] == null)
            {
                ViewState["BmInfoId"] = (long)0;
            }
            return (long)ViewState["BmInfoId"];
        }
        set
        {
            ViewState["BmInfoId"] = value;
        }
    }

    protected void BindData()
    {
        BmInfo bmInfo = BmInfo.FindById(this.BmInfoId);
        BindData(bmInfo);
    }

    protected void BindData(BmInfo bmInfo)
    {
        if (bmInfo != null)
        {
            this.lblStatus.Text = bmInfo.Status;

            this.txtIDCardNum.Text = bmInfo.IDCardNum;
            this.lblXH.Text = bmInfo.XH;

            this.txtName.Text = bmInfo.Name;
            this.ddlSex.SelectedValue = bmInfo.Sex;
            this.txtBirthday.Text = bmInfo.Birthday;
            this.txtTel.Text = bmInfo.Tel;
            this.txtSchool.Text = bmInfo.School;
            this.txtSchoolAddress.Text = bmInfo.SchoolAddress;
            this.txtBanji.Text = bmInfo.Banji;
            this.txtZhiWu.Text = bmInfo.ZhiWu;
            this.txtTeChang.Text = bmInfo.TeChang;
            this.ddlZhuXiao.SelectedValue = bmInfo.ZhuXiao;

            this.ddlHukouLeibie.SelectedValue = bmInfo.HukouLeibie;
            this.txtHukouAddress.Text = bmInfo.HukouAddress;
            this.txtHomeAddress.Text = bmInfo.HomeAddress;


            this.txtFatherName.Text = bmInfo.FatherName;
            this.txtFatherWork.Text = bmInfo.FatherWork;
            this.txtFatherTel.Text = bmInfo.FatherTel;
            this.txtFatherMobil.Text = bmInfo.FatherMobil;

            this.txtMotherName.Text = bmInfo.MotherName;
            this.txtMotherWork.Text = bmInfo.MotherWork;
            this.txtMotherTel.Text = bmInfo.MotherTel;
            this.txtMotherMobil.Text = bmInfo.MotherMobil;

            //成绩开始
            this.txtChinese1.Text = bmInfo.Chinese1;
            this.txtMath1.Text = bmInfo.Math1;
            this.txtEnglish1.Text = bmInfo.English1;

            this.txtChinese2.Text = bmInfo.Chinese2;
            this.txtMath2.Text = bmInfo.Math2;
            this.txtEnglish2.Text = bmInfo.English2;

            this.txtChinese3.Text = bmInfo.Chinese3;
            this.txtMath3.Text = bmInfo.Math3;
            this.txtEnglish3.Text = bmInfo.English3;

            this.txtChinese4.Text = bmInfo.Chinese4;
            this.txtMath4.Text = bmInfo.Math4;
            this.txtEnglish4.Text = bmInfo.English4;

            this.txtPrize.Text = bmInfo.Prize;

            this.txtBeizhu.Text = bmInfo.Beizhu;

            this.txtXm1.Text = bmInfo.Xm1;
            this.txtNation.Text = bmInfo.Nation;
            this.txtHealth.Text = bmInfo.Health;
            this.txtBmh.Text = bmInfo.Bmh;
            this.txtSmsMobile.Text = bmInfo.SmsMobile;

            this.rblXiangmu.ClearSelection();
            if (this.rblXiangmu.Items.FindByValue(bmInfo.Xiangmu) != null)
            {
                this.rblXiangmu.Items.FindByValue(bmInfo.Xiangmu).Selected = true ;
            }

            if (bmInfo.Status == "填写报名表")
            {
                this.btnSave.Enabled = true;
                this.btnSubmit.Enabled = true;

            }
            else
            {
                this.lblMsg.Text = "你的报名表已经提交，不能再修改，若必须要修改请准备材料在现场确认报名信息时修改。";
                this.btnSave.Enabled = false;
                this.btnSave.ToolTip = "你的报名表已经提交，不能再修改，若必须要修改请准备材料在现场确认报名信息时修改。";
                this.btnSubmit.Enabled = false;
                this.btnSubmit.ToolTip = "你的报名表已经提交，不能再修改，若必须要修改请准备材料在现场确认报名信息时修改。";

            }

            this.hlPrint.NavigateUrl = string.Format("frmPrint.aspx?Id={0}", bmInfo.Id);
        }
        else
        {
            this.btnSubmit.Enabled = true;
            this.lblStatus.Text = "填写报名表";

            this.hlPrint.Visible = false;

        }

        if (this.User.Identity.IsAuthenticated)
        {
            this.btnSave.Enabled = true;
            this.btnSubmit.Enabled = true;

        }
    }

    /// <summary>
    /// 提交或保存数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        this.lblMsg.Text = string.Empty;

        if (CheckCard.CheckIDCard(this.txtIDCardNum.Text) == false)
        {
            this.MessageBox("你输入的身份证号码有误，请检查！");
    //        return;
        }

        switch (this.txtIDCardNum.Text.Substring(16, 1))
        {
            case "1":
            case "3":
            case "5":
            case "7":
            case "9":
                if (this.ddlSex.SelectedValue != "男")
                {
                    this.MessageBox("你的性别与身份证号码中的性别标示不一致，请检查你的性别与身份证号码。");
         //           return;
                }
                break;
            case "0":
            case "2":
            case "4":
            case "6":
            case "8":
                if (this.ddlSex.SelectedValue != "女")
                {
                    this.MessageBox("你的性别与身份证号码中的性别标示不一致，请检查你的性别与身份证号码。");
       //             return;
                }
                break;
            default:
                break;
        }

        string strBirthdayForIDCard = this.txtBirthday.Text.Replace("-", "");
        if (strBirthdayForIDCard != this.txtIDCardNum.Text.Substring(6, 8))
        {
            //this.lblMsg.Text = JsUtil.MakeErrorAlertMessage(
            //    "你的出生日期与身份证号码中的出生日期不一致，请检查你的出生日期与身份证号码。"
            //    );
            this.MessageBox("你的出生日期与身份证号码中的出生日期不一致，请检查你的出生日期与身份证号码。");
        }



        BmInfo bmInfo = BmInfo.FindById(this.BmInfoId);

        if (bmInfo == null)
        {
            bmInfo = new BmInfo();
        }
        bmInfo.IDCardNum = this.txtIDCardNum.Text;
        bmInfo.Name = this.txtName.Text.Trim();
        bmInfo.Sex = this.ddlSex.SelectedValue;
        bmInfo.Birthday = this.txtBirthday.Text;
        bmInfo.Tel = this.txtTel.Text;

        bmInfo.School = this.txtSchool.Text;
        bmInfo.SchoolAddress = this.txtSchoolAddress.Text;
        bmInfo.Banji = this.txtBanji.Text;
        bmInfo.ZhiWu = this.txtZhiWu.Text;
        bmInfo.TeChang = this.txtTeChang.Text;
        bmInfo.ZhuXiao = this.ddlZhuXiao.SelectedValue;

        bmInfo.HukouLeibie = this.ddlHukouLeibie.SelectedValue;
        bmInfo.HukouAddress = this.txtHukouAddress.Text;
        bmInfo.HomeAddress = this.txtHomeAddress.Text;


        bmInfo.FatherName = this.txtFatherName.Text;
        bmInfo.FatherWork = this.txtFatherWork.Text;
        bmInfo.FatherTel = this.txtFatherTel.Text;
        bmInfo.FatherMobil = this.txtFatherMobil.Text;

        bmInfo.MotherName = this.txtMotherName.Text;
        bmInfo.MotherWork = this.txtMotherWork.Text;
        bmInfo.MotherTel = this.txtMotherTel.Text;
        bmInfo.MotherMobil = this.txtMotherMobil.Text;

        //成绩开始
        bmInfo.Chinese1 = this.txtChinese1.Text;
        bmInfo.Math1 = this.txtMath1.Text;
        bmInfo.English1 = this.txtEnglish1.Text;

        bmInfo.Chinese2 = this.txtChinese2.Text;
        bmInfo.Math2 = this.txtMath2.Text;
        bmInfo.English2 = this.txtEnglish2.Text;

        bmInfo.Chinese3 = this.txtChinese3.Text;
        bmInfo.Math3 = this.txtMath3.Text;
        bmInfo.English3 = this.txtEnglish3.Text;

        bmInfo.Chinese4 = this.txtChinese4.Text;
        bmInfo.Math4 = this.txtMath4.Text;
        bmInfo.English4 = this.txtEnglish4.Text;
        //成绩结束

        bmInfo.Prize = this.txtPrize.Text;

        bmInfo.Beizhu = this.txtBeizhu.Text;

        bmInfo.Xm1 = this.txtXm1.Text;
        bmInfo.Nation = this.txtNation.Text;
        bmInfo.Health = this.txtHealth.Text;
        bmInfo.Bmh = this.txtBmh.Text;
        bmInfo.SmsMobile = this.txtSmsMobile.Text;
        bmInfo.Xiangmu = this.rblXiangmu.SelectedValue;
        if (this.rblXiangmu.SelectedIndex < 5)
            bmInfo.Leibie = "体育";
        else if (this.rblXiangmu.SelectedIndex <7)
        {
            bmInfo.Leibie = "艺术";
        }
        else if (this.rblXiangmu.SelectedIndex < 9)
        {
            bmInfo.Leibie = "科技";
        }

        Button btn = (Button)sender;
        if (btn.Text == "保存" && String.IsNullOrEmpty(bmInfo.Status))
        {
            bmInfo.Status = "填写报名表";
        }
        else if (btn.Text == "提交" && String.IsNullOrEmpty(bmInfo.Status))
        {
            bmInfo.Status = "等待现场确认";
        }
        else if (btn.Text == "提交" && bmInfo.Status == "填写报名表")
        {
            bmInfo.Status = "等待现场确认";
        }

        ValidateHandler vh = new ValidateHandler();
        //     if (vh.ValidateObject(bmInfo))

        try
        {
            if (vh.ValidateObject(bmInfo))
            {
                bmInfo.Save();
                if (string.IsNullOrEmpty(bmInfo.XH))
                {
                    bmInfo.XH = DateTime.Now.Year.ToString() + bmInfo.Id.ToString().PadLeft(4, '0');
                    bmInfo.Save();
                }
            }
            if (!vh.IsValid)
            {
                string errMsg = string.Empty;
                foreach (var item in vh.ErrorMessages)
                {
                    errMsg += item.Value;
                }
                this.lblMsg.Text = JsUtil.MakeErrorAlertMessage(errMsg);
                this.lblMsg.Text += errMsg;
            }
            else
            {
                this.BmInfoId = bmInfo.Id;
                this.hlPrint.NavigateUrl = string.Format("frmPrint.aspx?Id={0}", bmInfo.Id);

                JsUtil.MessageBox(this.Page, string.Format("你的报名序号:{0},姓名：{1},身份证号码:{2}请按时去学校现场确认。", bmInfo.XH, bmInfo.Name, bmInfo.IDCardNum));
                this.lblMsg.Text = string.Format("你的报名序号:{0},姓名：{1},身份证号码:{2}请按时去学校现场确认。", bmInfo.XH, bmInfo.Name, bmInfo.IDCardNum);
                BindData();

                if (Request.IsAuthenticated)
                {
                    Logger.Default.Info(
                            string.Format("用户：{0}，操作：{1}，报名表信息：{2}", this.User.Identity.Name, btn.Text, bmInfo)
                        );
                }
                else
                {
                    Logger.Default.Info(
                            string.Format("用户：{0}，操作：{1}，报名表信息：{2}", bmInfo.XH + bmInfo.Name, btn.Text, bmInfo)
                        );
                }
            }
        }
        catch (Exception ex)
        {
            this.lblMsg.Text = JsUtil.MakeErrorAlertMessage(ex.Message);
            Logger.System.Error(ex.Message);
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
 //       if (CheckCard.CheckIDCard(this.txtSearchIDCardNum.Text) == false)
        {
   //         this.MessageBox("你输入的身份证号码有误，请检查！");
    //        return;
        }

        int flag = 0;
        Condition con = Condition.Empty;
        //if (!String.IsNullOrEmpty(this.txtSearchXH.Text))
        //{
        //    con &= CK.K["XH"] == this.txtSearchXH.Text.Trim();
        //    flag++;
        //}
        if (!String.IsNullOrEmpty(this.txtSearchName.Text))
        {
            con &= CK.K["Name"] == this.txtSearchName.Text.Trim();
            flag++;
        }
        if (!String.IsNullOrEmpty(this.txtSearchIDCardNum.Text))
        {
            con &= CK.K["IDCardNum"] == this.txtSearchIDCardNum.Text.Trim();
            flag++;
        }

        if (flag >= 2)
        {
            BmInfo bmInfo = BmInfo.FindOne(con);
            if (bmInfo != null)
            {
                this.BmInfoId = bmInfo.Id;
                this.BindData(bmInfo);

                if (bmInfo.Status == "等待特长测试")
                {
                    MessageBox(string.Format("{0}同学：你的报名状态为{1},请准时参加特长测试。", bmInfo.Name, bmInfo.Status));
                }
                else if (bmInfo.Status == "确定不录取")
                {
                    MessageBox(string.Format("{0}同学：你的报名状态为{1}，非常抱歉，谢谢您的支持。", bmInfo.Name, bmInfo.Status));
                }
                else
                {
                    MessageBox(string.Format("{0}同学：你的报名状态为{1}", bmInfo.Name, bmInfo.Status));
                }
                
                this.MultiView1.ActiveViewIndex = 0;

                Logger.Default.Info(
                    string.Format("报名查询,身份证号{0}，报名序号：{1}，姓名：{2}，查询成功！",
                    this.txtSearchIDCardNum.Text, bmInfo.XH, this.txtSearchName.Text)
                );
            }
            else
            {
                Logger.Default.Info(
                    string.Format("报名查询,身份证号{0}，姓名：{1}，查询失败！",
                    this.txtSearchIDCardNum.Text, this.txtSearchName.Text)
                );
                JsUtil.MessageBox(this, "没找到相关记录，请核对你输入的信息。");
            }
        }
        else
        {
            JsUtil.MessageBox(this, "三项至少要填写两项才可查询。");
        }


    }
}
